﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Xml;
using Xm.CustomDesigner.UI.Adorners;

namespace Xm.CustomDesigner.UI
{
    /// <summary>
    /// 自定义的节点类
    /// </summary>
    public class XmNode : ContentControl
    {

        internal Point Left { get; private set; }

        internal Point Right { get; private set; }

        internal List<XmLine> Lines { get; set; }

        internal Point? DragStartPoint { get; set; } = null;//拖动点

        internal XmCanvas? _canvas;

        internal event EventHandler? Changed;

        /// <summary>
        /// 自定义节点类的静态构造函数
        /// </summary>
        static XmNode()
        {
            // 设置默认样式--默认样式的文件夹命名必须为 Themes，资源类型必须为 Generic.xaml
            DefaultStyleKeyProperty.OverrideMetadata
                (typeof(XmNode),
                new FrameworkPropertyMetadata(typeof(XmNode)));
        }

        public XmNode()
        {
            this.Lines = new List<XmLine>();

            this.Loaded += OnLoaded;

            this.LayoutUpdated += OnLayoutUpdated;

        }

        /// <summary>
        /// 加载
        /// </summary>
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var canvas = this.FindParent<XmCanvas>();

            if (canvas != null)
            {
                _canvas = canvas;
            }


            //var layer = AdornerLayer.GetAdornerLayer(this);

            //if (layer != null && _canvas != null)
            //{

            //    var adorner = new CustomNodeAdorner(this);
            //    layer.Add(adorner);
            //}
        }

        /// <summary>
        /// 当前控件的布局（位置、大小等）更新时触发
        /// </summary>
        private void OnLayoutUpdated(object? sender, EventArgs e)
        {
            //获取当前控件的相对位置
            var left = Double.IsNaN(Canvas.GetLeft(this)) ? 0 : Canvas.GetLeft(this);//nan方法可以判断当前控件相对于父控件的左边距，如果没有设置则返回0
            var top = Double.IsNaN(Canvas.GetTop(this)) ? 0 : Canvas.GetTop(this);
            var right = left + this.ActualWidth;
            var bottom = top + this.ActualHeight;

            //获取中间点位置
            var midx = left + this.ActualWidth / 2;
            var midy = top + this.ActualHeight / 2;

            //组合两个坐标点，左点和右点
            var leftPont = new Point(left, midy);
            var rightPoint = new Point(right, midy);

            if (this.Left != leftPont || this.Right != rightPoint)
            {
                this.Left = leftPont;
                this.Right = rightPoint;

                //激发事件
                this.Changed?.Invoke(this, EventArgs.Empty);

            }
        }

        /// <summary>
        /// 鼠标右键按下
        /// </summary>
        protected override void OnPreviewMouseRightButtonDown(MouseButtonEventArgs e)
        {
            this.DragStartPoint = e.GetPosition(_canvas);

            e.Handled = true;//标记事件已处理
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            if (_canvas == null)
                return;

            //右键松开则不处理
            if (e.RightButton != MouseButtonState.Pressed)
                this.DragStartPoint = null;

            if (this.DragStartPoint.HasValue)
            {
                //创建装饰器（这个线条）
                var layer = AdornerLayer.GetAdornerLayer(this);

                if (layer != null)
                {
                    var xmLineAdorner = new XmLineAdorner(_canvas, this);

                    if (xmLineAdorner != null)
                    {
                        layer.Add(xmLineAdorner);

                        e.Handled |= true;
                    }
                }
            }
        }
    }
}
